﻿
$(function () {

  // product error images
  //$('.product-image').productImage();

  // modal open
  $(document.body).on('click', '.popup-link', function (e) {
    e.preventDefault();
    var $this = $(this);
    var $target = $($this.data('popup-target') || '<div><iframe frameborder="0" style="border: none; width: 100%; height: 460px;" src="' + $(this).attr('href') + '"></iframe></div>');
    $target.modalDialog({ width: $this.data('popup-width'), height: $this.data('popup-height') });
    return false;
  });


  // modal close
  $(document.body).on('click', '.ui-dialog-titlebar .close a', function (e) {
    e.preventDefault();
    $(this).closest('.ui-dialog').find('.ui-dialog-content').dialog('close');
    return false;
  });


  // cs modal open
  $(document.body).on('click', 'a[href*="customer-services"]', function (e) {
    e.preventDefault();
    var $this = $(this);
    var $customerServices = $('<div><iframe frameborder="0" style="border: none; width: 100%; height: 590px;" src="' + $(this).attr('href') + '"></iframe></div>');
    $customerServices.modalDialog({ width: 900, height: 'auto' });
    return false;
  });


  // popup ajax forms posting
  $('.product-action-form').on('submit', 'form', function (e) {
    e.preventDefault();
    var $this = $(this);
    if ($this.valid()) {
      $.post(
        this.action,
        $this.serialize(),
        function (data) {
          $this[0].reset();
          var $content = $this.closest('.ui-dialog-content');
          $content.children().hide();
          var $confirmation = $(data).appendTo($content);
          setTimeout(function () {
            $content.dialog('close').children().show();
            $confirmation.remove(); 
          }, 4000);
        });
    }
    return false;
  });
  

  // closable
  $('a[data-close-selector]').click(function (e) {
    e.preventDefault();
    var $this = $(this);
    var $remove = $this.closest($this.data('close-selector'));
    $remove.slideUp(function () { $remove.remove(); });
    return false;
  });

});
